import React from 'react';
import util from 'util';
import {Button, Table, Modal, Form} from 'antd';
import { SearchDemo} from 'kpl';
import sys_dict from "sys_dict";

class A301 extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tblSource: [],
            searchParams: {
                rolename: "",
                roletype: "",
                descript: "",
                "page": 1,
                "rows": sys_dict.defaultPageSize.toString()
            },
            totalCount: 0,
            isLoading: false
        }
    }

    componentDidMount() {
        let params = this.state.searchParams;
        this.refresh(params);
    }

    refresh(paramdic) {
        this.setState({isLoading: true});
        paramdic.release_begin_date = util.date2str(paramdic.release_begin_date);
        paramdic.release_end_date = util.date2str(paramdic.release_end_date);
        util.httpRequest({
            type: "POST", url: 'content030010.json', params: paramdic, plattype: '', success: function (result) {
                if (result.ret_code == "0000") {
                    this.setState({
                        tblSource: result.notice_list,
                        totalCount: result.total
                    });
                }
            }.bind(this),
            complete: function () {
                this.setState({
                    isLoading: false
                })
            }.bind(this)
        });
    }

    search(params) {
        console.log(this);
        params.page = "1";
        params.rows = sys_dict.defaultPageSize.toString();
        this.setState({
            searchParams: params
        });
        this.refresh(params);
    }

    onShowSizeChange(current, pageSize) {
        console.log(current, pageSize);
        var params = this.state.searchParams;
        params.rows = pageSize;
        params.page = current;
        this.setState({searchParams: params});
        this.refresh(params);
    }

    onChange(page) {
        console.log(page);
        var params = this.state.searchParams;
        params.page = page;
        this.setState({searchParams: params});
        this.refresh(params);
    }

    config() {
        const pagination = {
            total: this.state.totalCount,
            showSizeChanger: true,
            showQuickJumper: true,
            pageSizeOptions: sys_dict.pageSizeOptions,
            defaultPageSize: sys_dict.defaultPageSize,
            onShowSizeChange: this.onShowSizeChange.bind(this),
            onChange: this.onChange.bind(this)
        };

        return {
            tableConfig: {
                pagination: pagination,
                loading: this.state.isLoading,
                dataSource: this.state.tblSource,
                columns: [
	                {title: '标题',dataIndex: 'title',key: 'title'}, 
	                {title: '发布人',dataIndex: 'release_user_name',key: 'release_user_name'},
	                {title: '发布时间',dataIndex: 'release_datetime',key: 'release_datetime'},
	                {title: '操作',dataIndex: 'caozuo',
	                 	render: (text, data, index) => (
                        <span>
                               <span>
                                   <a onClick={this.updateRecord.bind(this, data, index)}>修改</a>
                                </span>
                                <span className="ant-divider"></span>
                                 <a onClick={this.deleteRecord.bind(this, data, index)}>删除</a>
                        </span>
                        )
	                }
                ]
            }
        };
    }

    updateRecord(record, type, e){

    }


	deleteRecord(record, type, e) {
		var me = this;
        var sContent="若将滚动消息:["+record.title+"] 删除则用户将无法查看到此消息，确认删除吗？";
        var sOkText="确认删除";
        var sCancelText="不删除";
        
        
        Modal.confirm({
        	     title:sOkText,
                content: sContent,
                okText: sOkText,
                cancelText: sCancelText,
                onOk: function () {                  
                    util.httpRequest({
                        type: "POST",
                        url: 'content030008.json',
                        plattype: '',
                        params: record,
                        success: function (result) {
                        	console.log("searchParams.release_begin_date:"+me.state.searchParams.release_begin_date);
                            me.refresh(me.state.searchParams);
                        }.bind(this)
                    });
                }
            });
		
	}
    handleCancel() {
        this.setState({
            modalVisible: false
        });
    }

    render() {
        let config = this.config();
        const searchConfig = {
            searchTitle: "发布消息查询",
            searchInit: {
               
            },
            searchField: [
                [
                    {propsname: "标题：", propsKey: "title", propsType: "input", key: "1"},
                    {propsname: "发布人姓名：", propsKey: "release_user_name", propsType: "select", key: "2"},
                    {propsname: "开始时间：", propsKey: "release_begin_date", propsType: "datepicker", key: "3"},
                    {propsname: "截止时间：", propsKey: "release_end_date", propsType: "datepicker", key: "4"}
                ]
            ]
        };

        return (
            <div>
                <SearchDemo search={this.search.bind(this)} searchConfig={searchConfig}></SearchDemo>
                <Table {...config.tableConfig}></Table>
            </div>
        );
    }
}

export default A301;